import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "@/plugins/element";

Vue.config.productionTip = false;

import "element-ui/lib/theme-chalk/index.css";

import axios from "axios";
Vue.prototype.$http = axios;

axios.defaults.baseURL = "http://127.0.0.1:8888/api/private/v1/";

axios.interceptors.request.use(config => {
  //config对象下有请求的信息
  //若存在token,则将token绑定在headers上
  if (sessionStorage.getItem("token")) {
    config.headers.Authorization = sessionStorage.getItem("token");
    return config;
  } else {
    return config;
  }
});

//注册一个全局的时间格式化过滤器
Vue.filter("dateFormat", function(timestamp) {
  const time = new Date(timestamp);
  let y = time.getFullYear();
  let m = (time.getMonth() + "").padStart(2, "0");
  let d = (time.getDay() + "").padStart(2, "0");
  let h = (time.getHours() + "").padStart(2, "0");
  let min = (time.getMinutes() + "").padStart(2, "0");
  let s = (time.getSeconds() + "").padStart(2, "0");

  return y + "-" + m + "-" + d + " " + h + "-" + min + "-" + s;
});
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
